package com.wzy.alllearning.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wzy.alllearning.domain.common.ResponseResult;
import com.wzy.alllearning.exception.BusinessException;
import com.wzy.alllearning.exception.ErrorCodeEnum;
import com.wzy.alllearning.service.FilesService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/api/files")
@Slf4j
public class FileController
{
	@Resource(name = "localFileServiceImpl")
	private FilesService filesService;

	@PostMapping("/upload")
	public ResponseResult<String> upload(@NotNull MultipartFile file)
	{
		try
		{
			filesService.upload(file.getInputStream(), file.getOriginalFilename());
		}
		catch (IOException e)
		{
			log.error("文件上传失败");
			throw new BusinessException(ErrorCodeEnum.FILE_UPLOAD_FAILURE, e);
		}
		return ResponseResult.success(file.getOriginalFilename());
	}
}
